<template>
  <div class="login">
    <div v-if="openLoginType" style="padding-top:20%;">
      {{ loginType }}
    </div>
  </div>
</template>
<script>
import {
  global
} from '../../filter/global-api'
import $ from 'jquery'
import _tools from '../../framework/commonJs/wptx_tools'
import _validate from '../../framework/commonJs/wptx_validate'
import _api from '../../framework/API/common'
import CryptoJS from 'crypto-js'
export default {
  data() {
    return {
      loginType: '',
      openLoginType: true,
      // loginUrl: process.env.VUE_APP_BASE_API + 'portal/#/sc',
      loginUrl: 'http://localhost:8088/item-front/'

    }
  },
  mounted() {
    const self = this
    let code = _tools.getUrlParamValue(window.location.href, 'code')
    let state = _tools.getUrlParamValue(window.location.href, 'state')
    code = 'asdfasdf'
    state = '21'
    // State:标记登录来源，0：PC企业微信扫码；1：企业微信APP；21：沃尔玛SSO
    // 当前系统支持登录方式：21：沃尔玛SSO
    if (_validate.checkEmpty(code)) {
      // code获取失败，请重新登录
      self.forwardToLogin('code获取失败，请重新登录')
      return
    }

    if (_validate.checkEmpty(state)) {
      // State获取失败，请联系管理员更新应用配置
      self.forwardToLogin('State获取失败，请联系管理员更新应用配置')
      return
    }

    if (state == 0) {
      // 当前系统暂未支持企业微信PC端扫码登录，请通过SSO或登录企业微信进入应用
      self.forwardToLogin('当前系统暂未支持企业微信PC端扫码登录，请通过SSO或登录企业微信进入应用')
      return
    }

    if (state == 2) {
      // 当前系统暂未支持沃尔玛sso登录，请通过企业微信应用进入
      self.forwardToLogin('当前系统暂未支持沃尔玛sso登录，请通过企业微信应用进入')
      return
    }

    if (state != 0 && state != 1 && state != 21) {
      // State标记有误，请联系管理员更新应用配置
      self.forwardToLogin('State标记有误，请联系管理员更新应用配置')
      return
    }
    self.init(code, state)
    _tools.info('state[' + state + ']Code[' + code + ']')
  },
  methods: {
    init(code, state) {
      const self = this
      const params = {
        code: self.decrypt(decodeURIComponent(code.replace(/%20/g, '+'))),
        state: state
      }
      // sessionStorage.setItem('token', params.code);
      _api.authLogin(self, params, self.callBack_authLogin)
    },
    callBack_authLogin(resp) {
      // userType: 用户所属系统，0：总部用户，1：门店用户，2：系统用户，3：供应商，4：外部用户
      // userIdentity: 0:营运；1：店总；2：店员；3：采购；4：财务
      const self = this
      if (_validate.checkEmpty(resp) || _validate.checkEmpty(resp.data)) {
        // 获取用户信息失败，请重新登录
        self.forwardToLogin('获取用户信息失败，请重新登录')
        return
      }
      const data = resp.data
      _tools.info('获取用户信息[---------' + JSON.stringify(data) + '------]')
      if (data === null) {
        // 获取用户信息失败，请重新登录
        self.forwardToLogin('获取用户信息失败，请重新登录')
        return
      }
      if (_validate.checkEmpty(data.loginSource)) {
        self.forwardToLogin('当前用户未授权登录此系统')
      } else if (data.loginSource == 0) {
        if (_validate.checkEmpty(data.loginType)) {
          self.forwardToLogin('没有获取到LoginType')
        } else if (data.loginType == 0) {
          if (_validate.checkEmpty(data.userType)) {
            self.forwardToLogin('没有获取到userType')
          } else if (data.userType == -1) {
            self.forwardToLogin('当前用户未授权登录此系统')
          } else if (data.userType == 0) {
            if (_validate.checkEmpty(data.businessIdentity)) {
              self.forwardToLogin('没有获取到businessIdentity')
            } else if (data.businessIdentity == 0) {
              if (data.dataRoleIdentity == 2) {
                self.$router.push('/item/global')
              } else {
                // self.$router.push('/item/item')
                self.$router.push('/basedata/brand')
              }
              sessionStorage.setItem('context', JSON.stringify(data))
              sessionStorage.setItem('token_crec', data.token)
              sessionStorage.setItem('token', data.ssoToken)
            } else if (data.businessIdentity == 1) {
              self.forwardToLogin('当前用户未授权登录此系统')
            }
          } else if (data.userType == 1) {
            self.forwardToLogin('当前用户暂未授权登录此系统')
          } else if (data.userType == 2) {
            self.forwardToLogin('当前用户暂未授权登录此系统')
          } else if (data.userType == 3) {
            self.forwardToLogin('当前用户暂未授权登录此系统')
          } else if (data.userType == 4) {
            self.forwardToLogin('当前用户暂未授权登录此系统')
          }
        } else if (data.loginType == 1) {
          self.forwardToLogin('当前应用暂未支持VendorSSO登录')
        } else if (data.loginType == 2) {
          self.forwardToLogin('当前应用暂未支持账户密码登录')
        } else if (data.loginType == 3) {
          self.forwardToLogin('当前应用暂未支持企业微信扫码登录')
        } else if (data.loginType == 4) {
          self.forwardToLogin('当前应用暂未支持企业微信登录')
        }
      } else if (data.loginSource == 1) {
        self.forwardToLogin('当前应用暂未支持移动端登录')
      }
    },
    // 错误提示
    forwardToLogin(title) {
      const self = this
      self.loginType = title
      self.openLoginType = true
      self.toSsoLogin()
    },
    // 页面URL替换
    toSsoLogin() {
      const self = this
      setTimeout(() => {
        window.location.replace(self.loginUrl)
      }, 3000)
    },
    // 解密
    decrypt(word, keyStr) {
      keyStr = keyStr || 'bf960145b2a1338f'
      const key = CryptoJS.enc.Utf8.parse(keyStr)
      const decrypt = CryptoJS.AES.decrypt(word, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      })
      return CryptoJS.enc.Utf8.stringify(decrypt).toString()
    }
  }
}
</script>
<style scoped>
.rt {
  float: right;
  margin-bottom: 10px;
}

.login {
  width: 100%;
  height: 100%;
  /* background-color: #1D2E55; */
  /* display:flex; */
  justify-content: center;
  text-align: center;
}

.login-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}

.login-form {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 200px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}

img {
  width: 80px;
  height: 80px;
}

.fist {
  position: fixed;
  bottom: -20px;
  left: -10px;
  z-index: 999;
  background-color: transparent;
  border: 0;
  color: transparent;
}

.fist:hover {
  position: fixed;
  bottom: 0px;
  left: 0px;
  background-color: #50bfff;
  color: #fff;
}
</style>
